﻿app.controller('SafetyCostController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetDepartmentList = function (id) {
        $scope.DepartmentList = [];
        $http({
            method: 'POST',
            url: '../CheckForm/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.DepartmentList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetOperateLog = function (id) {
        $scope.OperateLog = [];
        $http({
            method: 'POST',
            url: '../SafetyCost/GetOperateLog',
            data: { "Id": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.OperateLog = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.GetUsageRangeList = function (id) {
        $scope.UsageRangeList = [];
        $http({
            method: 'POST',
            url: '../SafetyCost/GetUsageRangeList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.UsageRangeList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.GetSafetyCostOption = function (id) {
        $scope.CostOption = [];
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetyCostOption'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.CostOption = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.SetCurrentIndex = function (index) {
        $scope.CurrentIndex = index;
    }

    $scope.SelectUsageRange = function (index, list) {
        var controls = $('input[name="usagerangelist"]');
        var id = "";
        for (let i = 0; i < controls.length; i++) {
            if ($(controls[i]).prop("checked")) {
                id = $(controls[i]).val();
            }
        }
        list[index]["RangeId"] = id;
        var text = "";
        for (let i = 0; i < $scope.UsageRangeList.length; i++) {
            if ($scope.UsageRangeList[i].Id == id) {
                $("#range_" + index).html($scope.UsageRangeList[i].Title + "." + $scope.UsageRangeList[i].Content);
            }
        }

        console.log(list);
    }

    $scope.AddApplyItem = function (items) {
        var obj = {
            "Index": items.length,
            "Amount": 0
        };
        items.push(obj);
    }


    $scope.AddSafetyCostNotice = function (model, status) {
        model.Status = status;
        var department = [];
        var deplist = $('[name=DepartmentSelected]');
        for (var i = 0; i < deplist.length; i++) {
            if ($(deplist[i]).prop("checked")) {
                department.push(deplist[i].id);
            }
        }
        if (model.Status == 1) {
            if (model.Title == null || model.Title == "") {
                alertify.alert("请填写通知标题");
                return false;
            }


        }
        var editorInstance = CKEDITOR.instances['noticecontent'];
        model.NoticeContent = editorInstance.getData();
        var files = document.getElementById("taskfile").files;

        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCost/AddSafetyCostNoticeForm',
            data: { 'model': model, 'Department': department },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "BudgetNotificationList?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.EditSafetyCostNoticeForm = function (model, status, page) {
        model.Status = status;
        var department = [];
        var deplist = $('[name=DepartmentSelected]');
        for (var i = 0; i < deplist.length; i++) {
            if ($(deplist[i]).prop("checked")) {
                department.push(deplist[i].id);
            }
        }
        if (model.Status == 1) {
            if (model.Title == null || model.Title == "") {
                alertify.alert("请填写通知标题");
                return false;
            }


        }
        var editorInstance = CKEDITOR.instances['noticecontent'];
        model.NoticeContent = editorInstance.getData();
        var files = document.getElementById("taskfile").files;

        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCost/EditSafetyCostNoticeForm',
            data: { 'model': model, 'Department': department },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "BudgetNotificationList?page=" + page;
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.SelectAll = function (obj, name) {
        if ($("#Department_All").prop("checked")) {
            var list = $('[name="' + name + '"]');
            for (var i = 0; i < list.length; i++) {
                $(list[i]).prop("checked", true);
            }
        }
        else {
            var list = $('[name="' + name + '"]');
            for (var i = 0; i < list.length; i++) {
                $(list[i]).prop("checked", false);
            }
        }

    }

    $scope.GetNoticeFormPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../SafetyCost/GetNoticeFormPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetNoticeFormDetail = function (id) {
        $scope.NoticeModel = {};
        $http({
            method: 'POST',
            url: '../SafetyCost/GetNoticeFormDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.NoticeModel = ret.Data;
                    $("#noticecontent").html($scope.NoticeModel.NoticeContent);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetNoticeFormEdit = function (id) {
        $scope.NoticeModel = {};
        $http({
            method: 'POST',
            url: '../SafetyCost/GetNoticeFormDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.NoticeModel = ret.Data;
                    $scope.NoticeModel.CreateTime = $filter('JSONDate')($scope.NoticeModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.NoticeModel.PublishTime = $filter('JSONDate')($scope.NoticeModel.PublishTime, 'yyyy-MM-dd');
                    $scope.NoticeModel.EndTime = $filter('JSONDate')($scope.NoticeModel.EndTime, 'yyyy-MM-dd');
                    $("#noticecontent").html($scope.NoticeModel.NoticeContent);
                    $scope.NoticeModel.Years = $scope.NoticeModel.Years + "";

                    setTimeout(function () {
                        for (let i = 0; i < ret.Data.Departments.length; i++) {
                            $("#" + ret.Data.Departments[i]).prop("checked", true);
                        }
                        var editorInstance = CKEDITOR.instances['noticecontent'];
                        editorInstance.setData($scope.NoticeModel.NoticeContent);
                    }, 2000);


                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetSafetyFormFile = function (id) {
        $scope.FileList = {};
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetyFormFile',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.FileList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.CalcTotalApplyAmount = function (list) {
        $scope.TotalAmount = 0;
        for (var i = 0; i < list.length; i++) {
            $scope.TotalAmount += list[i].Amount;
        }
    }

    $scope.RemoveApplyItems = function (item, list) {
        let index = list.indexOf(item);
        list.splice(index, 1);
    }

    $scope.AddBudgetApply = function (model, status, items) {
        model.Status = status;
        model.TotalAmount = $scope.TotalAmount;
        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        var files = document.getElementById("taskfile").files;
        Upload.upload({
            method: 'POST',
            url: '../SafetyCost/AddBudgetApply',
            data: { 'model': model, 'items': items },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "BudgetApplyList?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.GetSafetyCostApplyPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetyCostApplyPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetAllUserList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetAllUserList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue == n.UserNo)
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddSafetyCostApplyForm = function (model, items, status) {
        model.Status = status;
        model.TotalAmount = $scope.TotalAmount;
        for (let i = 0; i < items.length; i++) {
            let name = "OptionSelect_" + i;
            var list = $('[name="' + name + '"]');
            for (var j = 0; j < list.length; j++) {
                if ($(list[j]).prop("checked")) {
                    items[i]["OptionId"] = $(list[j]).val();
                }
            }
        }
        var files = document.getElementById("taskfile").files;
        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCost/AddSafetyCostApplyForm',
            data: { 'model': model, 'items': items },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "CostRecordList?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.EditBudgetApply = function (model, status, items, page) {
        model.Status = status;
        model.TotalAmount = $scope.TotalAmount;
        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        var files = document.getElementById("taskfile").files;
        Upload.upload({
            method: 'POST',
            url: '../SafetyCost/EditBudgetApply',
            data: { 'model': model, 'items': items },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "BudgetApplyList?page=" + page;
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.GetSafetCostApplyFormList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetCostApplyFormList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetSafetyCostApplyDetail = function (id) {
        $scope.CostDetail = {};
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetyCostApplyDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.CostDetail = ret.Data;
                    $scope.TotalAmount = $scope.CostDetail.TotalAmount;
                    $scope.GetAllUserList('approve_list', ret.Data.ApproveUser);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetSafetyBudgetApplyDetail = function (id) {
        $scope.BudgetDetail = {};
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetyBudgetApplyDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.BudgetDetail = ret.Data;
                    $scope.TotalAmount = $scope.BudgetDetail.TotalAmount;
                    $scope.BudgetDetail.Years = $scope.BudgetDetail.Years + "";
                    $scope.BudgetDetail.CreateTime = $filter('JSONDate')($scope.BudgetDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.BudgetDetail.ApplyDate = $filter('JSONDate')($scope.BudgetDetail.ApplyDate, 'yyyy-MM-dd');
                    //$scope.GetAllUserList('approve_list', ret.Data.ApproveUser);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetBudgetStatusDetail = function (id) {
        $scope.BudgetStatusDetail = {};
        $http({
            method: 'POST',
            url: '../SafetyCost/GetBudgetStatusDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.BudgetStatusDetail = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetCostStatusDetail = function (id) {
        $scope.CostStatusDetail = {};
        $http({
            method: 'POST',
            url: '../SafetyCost/GetCostStatusDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.CostStatusDetail = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.SafetyCostApprove = function (Id, isapprove, remark, page) {
        var text = "驳回";
        if (isapprove) {
            text = "同意";
        }
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认" + text + "该申请？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/SafetyCostApprove',
                    data: { 'Id': Id, "IsApprove": isapprove, "Remark": remark, "Type": 1 },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                location.href = "CostRecordList?page=" + page;
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.ShowCostList = function (list) {
        $scope.CostList = list;
        $scope.totalAmount = 0;
        for (var i = 0; i < list.length; i++) {
            $scope.totalAmount += list[i].Amount;
            list[i].CreateTime = $filter('JSONDate')(list[i].CreateTime, 'yyyy-MM-dd HH:mm:ss');
        }
        $scope.CalcTotal(list);
    }

    $scope.CalcTotal = function (list) {
        $scope.TotalCost = 0;
        for (var i = 0; i < list.length; i++) {
            $scope.TotalCost += list[i].CostAmount;
        }

    }

    $scope.SafetyCostApprove = function (model, flag, pagename, page) {
        model.IsApprove = flag;
        if (model.IsApprove == false && (model.Remark == null || model.Remark == "")) {
           
        }
        $http({
            method: 'POST',
            url: '../SafetyCost/SafetyCostApprove',
            data: { 'model': model }
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    $scope.AlertBox(ret.Msg);
                    setTimeout(function () {
                        location.href = pagename + "?page=" + page;
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
    $scope.MBudgetApprove = function (model, flag, pagename) {
        model.IsApprove = flag;
        var text = "驳回";
        if (model.IsApprove) {
            text = "同意";
        }
     
        mui.confirm("确认" + text + "该申请单？", '系统提醒', function (e) {
            if (e.index == 1) {
                $http({
                    method: 'POST',
                    url: '../SafetyCost/SafetyCostApprove',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            mui.toast(ret.Msg);
                            setTimeout(function () {
                                location.href = pagename;
                            }, 2000);
                        } else
                            mui.toast(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }
        })
    }

    $scope.MCostApprove = function (model, flag, pagename) {
        model.IsApprove = flag;
        var text = "驳回";
        if (model.IsApprove) {
            text = "同意";
        }
        if ($scope.CostStatusDetail.FlowInfo.CompleteStatus != null && $scope.ApproveModel.IsComplete == 'false') {
            var userdata = $scope.name.split('-');
            if (userdata.length != 2) {
                mui.alert("请选择下一节点审批人");
                return false;
            }
            model.ApproveUser = userdata[0];
        }
        mui.confirm("确认" + text + "该申请单？", '系统提醒', function (e) {
            if (e.index == 1) {
                $http({
                    method: 'POST',
                    url: '../SafetyCost/SafetyCostApprove',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            mui.toast(ret.Msg);
                            setTimeout(function () {
                                location.href = pagename;
                            }, 2000);
                        } else
                            mui.toast(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }
        })
    }

    $scope.UpdateCostItems = function (id, items, model, page) {
        $http({
            method: 'POST',
            url: '../SafetyCost/UpdateSafetyCost',
            data: { 'items': items, 'Id': id }
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    $scope.AlertBox(ret.Msg);
                    setTimeout(function () {
                        $scope.GetSafetCostApplyFormList(model, page);
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.DeleteNoiceForm = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该通知？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/DeleteNoiceForm',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetNoticeFormPageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.RecallNoticeForm = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认撤回该通知？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/RecallNoticeForm',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetNoticeFormPageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.DeleteSafetyCostApply = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该申请？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/DeleteSafetyCostApply',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetSafetCostApplyFormList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.DeleteSafetyBudgetApply = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该申请？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/DeleteSafetyBudgetApply',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetSafetyCostApplyPageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }
    $scope.RecallSafetyBudgetApply = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认撤回该申请？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/RecallSafetyBudgetApply',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetSafetyCostApplyPageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.DeleteCostApplyItems = function (model, list, newlist) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该项？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/DeleteCostApplyItems',
                    data: { 'Id': model.Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            let index = list.indexOf(model);
                            list.splice(index, 1);
                            $scope.CalcCostApplyEditTotal(list, newlist);

                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.DeleteBudgetApplyItems = function (model, list, newlist) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该项？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/DeleteBudgetApplyItems',
                    data: { 'Id': model.Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            let index = list.indexOf(model);
                            list.splice(index, 1);
                            $scope.CalcBudgetApplyEditTotal(list, newlist);

                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.DeleteSafetyCostFile = function (model, list) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该文件？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/DeleteSafetyCostFile',
                    data: { 'FileId': model.FileId },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            let index = list.indexOf(model);
                            list.splice(index, 1);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.RecallSafetyCostApply = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认撤回该申请？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCost/RecallSafetyCostApply',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                location.href = "CostRecordList?page=" + page;
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.ShowRangeInfo = function (list) {
        $scope.CurrentItems = list;
    }

    $scope.CalcCostApplyEditTotal = function (oldlist, newlist) {
        $scope.TotalAmount = 0;
        for (var i = 0; i < oldlist.length; i++) {
            $scope.TotalAmount += oldlist[i].Amount;
        }
        if (newlist != null) {
            for (var i = 0; i < newlist.length; i++) {
                $scope.TotalAmount += newlist[i].Amount;
            }
        }
    }
    $scope.CalcBudgetApplyEditTotal = function (oldlist, newlist) {
        $scope.TotalAmount = 0;
        for (var i = 0; i < oldlist.length; i++) {
            $scope.TotalAmount += oldlist[i].Amount;
        }
        if (newlist != null) {
            for (var i = 0; i < newlist.length; i++) {
                $scope.TotalAmount += newlist[i].Amount;
            }
        }
    }
    $scope.EditSafetyCostApplyForm = function (model, items, status) {
        model.Status = status;
        model.TotalAmount = $scope.TotalAmount;
        for (let i = 0; i < items.length; i++) {
            let name = "OptionSelect_" + i;
            var list = $('[name="' + name + '"]');
            for (var j = 0; j < list.length; j++) {
                if ($(list[j]).prop("checked")) {
                    items[i]["OptionId"] = $(list[j]).val();
                }
            }
        }
        var files = document.getElementById("taskfile").files;
        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCost/EditSafetyCostApplyForm',
            data: { 'model': model, 'items': items },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "CostRecordList?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
    $scope.refreshOption = function (control) {
        $("#" + control).selectpicker('refresh');
    }
    $scope.GetAllUserList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetAllUserList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue.includes(n.Id + ""))
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    //////////////////////移动端////////////////////////////////
    $scope.GetNodeMenu = function (node) {
        mui.showLoading("正在加载..", "div");
        var hmenu = sessionStorage.getItem('SafetyCostMenu');
        if (hmenu == null) {
            $http({
                method: 'POST',
                url: '../Account/GetMMenuList',
                data: { 'Node': node }
            })
                .success(function (ret) {
                    mui.hideLoading();
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        $scope.CostMenuList = ret.Data;
                        sessionStorage.setItem("SafetyCostMenu", JSON.stringify(ret.Data));
                    }
                    else {
                        mui.toast(ret.Msg);
                    }
                })
                .error(function (data, status, headers, config) {
                    mui.toast("请检查网络");
                });
        }
        else {
            $scope.CostMenuList = JSON.parse(hmenu);
            mui.hideLoading();
        }
    }
    $scope.InitNoticeFormPageList = function () {
        $scope.PageIndex = 1;
        mui.init({
            pullRefresh: {
                container: '#pullrefresh',
                down: {
                    style: 'circle',
                    auto: true,//可选,默认false.首次加载自动上拉刷新一次
                    callback: function () {
                        setTimeout(function () {
                            $scope.MGetNoticeFormPageList($scope.SearchModel, 1);
                            mui('#pullrefresh').pullRefresh().refresh(true);
                            mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
                        }, 1500);

                    }
                }, up: {
                    auto: false,
                    contentrefresh: '正在加载...',
                    callback: function () {
                        $scope.MGetNoticeFormPageList($scope.SearchModel, $scope.PageIndex + 1);
                    },

                }
            }
        });
    }

    $scope.GoPage = function (link) {
        location.href = link;
    }

    $scope.MGetNoticeFormPageList = function (model, page) {
        if (page == 1) {
            $scope.DataList = [];
        }
        $scope.PageIndex = page;
        $http({
            method: 'POST',
            url: '../SafetyCost/GetNoticeFormPageList',
            data: { 'model': model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    for (var i = 0; i < ret.Data.Data.length; i++) {
                        $scope.DataList.push(ret.Data.Data[i]);
                    }
                    if (ret.Data.Data.length == 0) {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
                    }
                    else {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
                    }
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.InitBudgetApplyFormPageList = function () {
        $scope.PageIndex = 1;
        mui.init({
            pullRefresh: {
                container: '#pullrefresh',
                down: {
                    style: 'circle',
                    auto: true,//可选,默认false.首次加载自动上拉刷新一次
                    callback: function () {
                        setTimeout(function () {
                            $scope.MGetSafetyCostApplyPageList($scope.SearchModel, 1);
                            mui('#pullrefresh').pullRefresh().refresh(true);
                            mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
                        }, 1500);

                    }
                }, up: {
                    auto: false,
                    contentrefresh: '正在加载...',
                    callback: function () {
                        $scope.MGetSafetyCostApplyPageList($scope.SearchModel, $scope.PageIndex + 1);
                    },

                }
            }
        });
    }

    $scope.MGetSafetyCostApplyPageList = function (model, page) {
        if (page == 1) {
            $scope.DataList = [];
        }
        $scope.PageIndex = page;
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetyCostApplyPageList',
            data: { 'model': model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    for (var i = 0; i < ret.Data.Data.length; i++) {
                        $scope.DataList.push(ret.Data.Data[i]);
                    }
                    if (ret.Data.Data.length == 0) {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
                    }
                    else {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
                    }
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });

    }
    $scope.InitCostApplyFormPageList = function () {
        $scope.PageIndex = 1;
        mui.init({
            pullRefresh: {
                container: '#pullrefresh',
                down: {
                    style: 'circle',
                    auto: true,//可选,默认false.首次加载自动上拉刷新一次
                    callback: function () {
                        setTimeout(function () {
                            $scope.MCostRecordFormList($scope.SearchModel, 1);
                            mui('#pullrefresh').pullRefresh().refresh(true);
                            mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
                        }, 1500);

                    }
                }, up: {
                    auto: false,
                    contentrefresh: '正在加载...',
                    callback: function () {
                        $scope.MCostRecordFormList($scope.SearchModel, $scope.PageIndex + 1);
                    },

                }
            }
        });
    }
    $scope.MCostRecordFormList = function (model, page) {
        if (page == 1) {
            $scope.DataList = [];
        }
        $scope.PageIndex = page;
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetCostApplyFormList',
            data: { 'model': model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    for (var i = 0; i < ret.Data.Data.length; i++) {
                        $scope.DataList.push(ret.Data.Data[i]);
                    }
                    if (ret.Data.Data.length == 0) {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
                    }
                    else {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
                    }
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.InitCostApplyOperate = function (model) {
        $scope.btnlist = [];
        if (model != null) {
            if (model.IsShowApprove) {
                var obj_approve = {
                    "name": "审核",
                    "link": "../SafetyCost/MBudgetApprove?Id=" + model.Id
                };
                $scope.btnlist.push(obj_approve);
            }
            var obj_detail = {
                "name": "详细",
                "link": "../SafetyCost/MBudgetApplyDetail?Id=" + model.Id
            };
            $scope.btnlist.push(obj_detail);
        }
        mui('#operate').popover('toggle');
    }

    $scope.InitCostRecordOperate = function (model) {
        $scope.btnlist = [];
        if (model != null) {
            if (model.IsShowApprove) {
                var obj_approve = {
                    "name": "审核",
                    "link": "../SafetyCost/MCostApprove?Id=" + model.Id
                };
                $scope.btnlist.push(obj_approve);
            }
            var obj_detail = {
                "name": "详细",
                "link": "../SafetyCost/MCostApplyDetail?Id=" + model.Id
            };
            $scope.btnlist.push(obj_detail);
        }
        mui('#operate').popover('toggle');
    }
    $scope.GetUserByName = function (name) {
        $scope.resultlist = [];
        for (let i = 0; i < $scope.AllUserList.length; i++) {
            if ($scope.AllUserList[i].UserName.includes(name) || $scope.AllUserList[i].UserNo.includes(name)) {
                $scope.resultlist.push($scope.AllUserList[i]);
            }
            if ($scope.resultlist.length > 5) {
                break;
            }
        }
    }
    $scope.selectuser = function (model) {
        $scope.name = model.UserNo + "-" + model.UserName;
        $scope.resultlist = [];
    }
    $scope.GetAllUserList = function () {
        if ($scope.AllUserList != null && $scope.AllUserList.length > 0) {
            return false;
        }
        $scope.AllUserList = [];
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Construction/GetAllUserList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.AllUserList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.MGetSafetyCostApplyDetail = function (id) {
        mui.showLoading("正在加载..", "div");
        $scope.CostDetail = {};
        $http({
            method: 'POST',
            url: '../SafetyCost/GetSafetyCostApplyDetail',
            data: { 'Id': id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                mui.hideLoading();
                if (ret.Code == 100) {
                    $scope.CostDetail = ret.Data;
                    $scope.TotalAmount = $scope.CostDetail.TotalAmount;
                }
                else {
                    mui.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
})

app.filter('TransCurrency', function () { //可以注入依赖
    return function (num) {
        var fraction = ['角', '分'];
        var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
        var unit = [
            ['元', '万', '亿'],
            ['', '拾', '佰', '仟']
        ];
        var head = num < 0 ? '欠' : '';
        num = Math.abs(num);

        var s = '';

        for (var i = 0; i < fraction.length; i++) {
            s += (digit[Math.floor(num * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        s = s || '整';
        num = Math.floor(num);

        for (var i = 0; i < unit[0].length && num > 0; i++) {
            var p = '';
            for (var j = 0; j < unit[1].length && num > 0; j++) {
                p = digit[num % 10] + unit[1][j] + p;
                num = Math.floor(num / 10);
            }
            s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
        }

        return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
    }
});